<template>
	<view class="container">
		<navSearchBar title="个人资料" ></navSearchBar>
		<view class="info flex-row-center">
			<image class="photo" :src="photo" mode=""></image>
			<view class="photo-change flex-row-center" @click="changeAvatar">
				<text>点击更换</text>
				<image src="@/static/personal/right.png" mode="aspectFill"></image>
			</view>
		</view>
		<view class="data">
			<view class="data-item">
				<view class="label">联系电话</view>
				<input class="my-input" tpye="number" v-model="phone" />
			</view>
			<view class="data-item">
				<view class="label">服务宣言</view>
				<textarea class="my-textarea" v-model="declaration"></textarea>
			</view>
			<view class="data-item">
				<view class="label">服务客户</view>
				<view class="tag flex-row-center">
					<view class="tag-item ellipsis" v-for="item in list" :key="item.id" @click="operate(item)">{{item.client_name}}</view>
				</view>
				<tn-action-sheet v-model="actionShow" :list="actionList" @click="choose"></tn-action-sheet>
				<uni-popup ref="popup" type="bottom">
					<view class="pop-box">
						<view class="pop-title">修改服务客户</view>
						<input class="my-input" type="text" v-model="customer" placeholder="客户名称">
						<view class="pop-operate flex-col-between">
							<button class="form-btn" @click="handleFix">确定修改</button>
							<button class="cancle" @click="handleCancle">取消</button>
						</view>
					</view>
				</uni-popup>
			</view>
		</view>
		<button class="confirm form-btn" @click="confirm">提交</button>
	</view>
</template>

<script>
	import {telReg} from "@/common/util/reg.js"
	export default {
		data() {
			return {
				photo:"",
				phone: "",
				declaration: "",
				list:[],
				customer:"",
				currentTag:{},
				actionShow: false,
				actionList: [{
						text: "修改客户",
						fontSize: 32,
						color:'#525252'
					},
					{
						text: "删除客户",
						fontSize: 32,
						color:'#525252'
					}
				]
			};
		},
		onLoad() {
			this.getUserInfo()
			this.getCustomerInfo()
		},
		methods: {
			async getUserInfo() {
				let res = await this.$api.userInfo()
				console.log(res, "res")
				const {avatar,username,detail} = res.data.data
				this.photo = avatar
				this.phone = username
				this.declaration = detail.service_declaration
			},
			async getCustomerInfo() {
				let res = await this.$api.customerInfo()
				this.list =res.data.data
			},
			changeAvatar() {
				uni.chooseImage({
					count: 1, 
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: (res) => {
						console.log(res,"res1")
						// uni.uploadFile({
						// 	url: `${this.$api.baseUrl}/api/file/avatar`,
						// 	filePath: res.tempFilePaths[0],
						// 	name: 'file',
						// 	header:{
						// 		'cx-auth-token':'ccd21a11d0381d7759497c8b346a037f'
						// 	},
						// 	success: (uploadFileRes) => {
						// 		this.photo = JSON.parse(uploadFileRes.data).data
						// 	},
						// 	fail:(res) =>{
						// 		console.log(res,"fail")
						// 	}
						// })
						
						this.$http.upload('/api/file/avatar',{filePath:res.tempFilePaths[0], name: 'file'}).then((uploadFileRes) => {
							console.log('fanhui',uploadFileRes)
							this.photo = uploadFileRes.data.data;
							 
							this.$api.editUser({
								avatar:uploadFileRes.data.data,
							})
						})
					}
				});
			},
			operate(item) {
				this.actionShow = true
				this.currentTag = item
				this.customer = item.client_name
			},
			choose(index) {
				if(index == 0) {
					this.actionShow = false
					this.$refs.popup.open()
				}else {
					this.handleDel()
				}
			},
			async handleFix() {
				let par = {
					id:this.currentTag.id,
					client_name:this.customer
				}
				let res = await this.$api.editCustomer(par)
				if(res.data.status == 200) {
					this.$tip.success("客户修改成功")
					this.getCustomerInfo()
					this.$refs.popup.close()
				}else {
					this.$tip.alert(res.data.message)
				}
			},
			handleCancle() {
				this.$refs.popup.close()
			},
			async handleDel() {
				let res = await this.$api.delCustomer({id:this.currentTag.id})
				if(res.data.status == 200) {
					this.$tip.success("客户删除成功")
					this.getCustomerInfo()
					this.$refs.popup.close()
				}else {
					this.$tip.alert("客户删除失败")
				}
			},
			async confirm() {
				if(!telReg(this.phone)){
					this.$tip.alert("请输入正确的电话格式")
					return
				}
				let par = {
					avatar:this.photo,
					username:this.phone,
					service_declaration:this.declaration
				}
				let res = await this.$api.editUser(par)
				if(res.data.status == 200) {
					this.$tip.success("编辑成功")
					uni.navigateBack(-1)
				}else {
					console.log('zamele')
					this.$tip.toast(res.data.message)
					// this.$tip.alert(res.data.message)//不能显示很多字
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		--btn-width: 282rpx;
		--textarea-height: 202rpx;
	}

	.info {
		margin: 0 30rpx;
		padding: 30rpx 0 20rpx;
		border-bottom: 2rpx solid #EFEFEF;

		.photo {
			width: 90rpx;
			height: 90rpx;
			border-radius: 50%;
			margin-right: auto;
		}

		.photo-change {
			text {
				font-size: $text-size;
				font-weight: Regular;
				color: #6F6F6F;
				margin-right: 20rpx;
			}

			image {
				width: 10rpx;
				height: 20rpx;
			}
		}
	}

	.data {
		padding: 40rpx 30rpx 0;

		.data-item {
			margin-bottom: 40rpx;

			.label {
				font-size: $text-size;
				font-weight: Regular;
				color: #6F6F6F;
				margin-bottom: 40rpx;
			}

			.tag {
				flex-wrap: wrap;

				.tag-item {
					width: 158rpx;
					height: 72rpx;
					line-height: 72rpx;
					margin-right: 20rpx;
					margin-bottom: 20rpx;
					text-align: center;
					padding: 0 24rpx;
					box-sizing: border-box;
					font-size: 24rpx;
					background: #F7F8FC;
					border-radius: 10rpx;
				}
			}
		}
	}

	.confirm {
		position: fixed;
		left: 50%;
		bottom: 40rpx;
		transform: translateX(-50%);
	}

	
	.pop-box {
		width: 100%;
		height: 380rpx;
		background: #FFFFFF;
		border-radius: 10rpx 10rpx 0 0 ;
		padding: 60rpx 70rpx 0;
		.pop-title {
			margin-bottom: 60rpx;
			font-size: 32rpx;
			line-height: 32rpx;
			font-weight: $Semibold;
			color: #525252;
			text-align: center;
		}
		.pop-operate {
			--btn-width:200rpx;
			--btn-height:56rpx;
			margin-top: 40rpx;
			.cancle {
				width: 200rpx;
				height: 56rpx;
				line-height: 56rpx;
				text-align: center;
				font-size: 32rpx;
				border-radius: 90rpx;
				border: 2rpx solid #7D7D7D;
				color:#7D7D7D;
				background: #FFFFFF;
				box-sizing: border-box;
				cursor: pointer;
			}
		}
	}
	input {
		font-size: 28rpx;
		color: #525252;
	}
</style>